﻿/// <reference path="sortable.js" />
/*
   get Employee for left pane and salary head 
   function run two time first time for employee and second time for salary head
*/
function getEmployee(type) {
    var emptypeid = document.getElementById('ddlEmpType');
    var period = document.getElementById('ddlPeriod');
    var period_val = "r"
    if (period) {
        period_val = period.value;
    }
    var _empval = 0
    var _id = 0
    _id = get_id();
    if (parseInt(_id) == 0) {
        if (emptypeid) {
            if (emptypeid.value != "") {
                _empval = emptypeid.value;
                document.getElementById('hdnEmpType').value = emptypeid.value;
                if (document.getElementById('ddlMonth')) {
                    document.getElementById('hdnMonth').value = document.getElementById('ddlMonth').value;
                }
                document.getElementById('hdnPeriod').value = period_val;
            }
            else {
                alert("Please select salary plan.");
                return;
            }
        }
    }
    if (document.getElementById("ddlMonth")) {
        if (document.getElementById("ddlMonth").value == "0") {
            alert("Please select month.");
            document.getElementById("ddlMonth").focus();
            return;
        }
    }
    var incloan=0;
    var incwd=0;
    if (type == "e") {
        senddata_post("SalaryProcess", "T=getemployee_sal&Empid=" + _empval + "&id=" + _id + "&month=" + document.getElementById("ddlMonth").value + "&incloan=" + get_check_val('includeloanamount') + "&incwd=" + get_check_val('includeworkingdays'), "salgrid")
    }
    else {
        senddata_post("SalaryProcess", "T=SalaryHead&Empid=" + _empval + "&Period=" + period_val + "&id=0", "SalarySheet")
    }
    if (document.getElementById('Paging')) {
        document.getElementById('Paging').innerHTML = '';
    }
}
function get_check_val(id)
{
   var val = 0;
   if (document.getElementById(id)) {
      if(document.getElementById(id).checked) {
         val=1;
      }
   }
   return val;
}
/*
Fix helper used by sort function.
*/
var fixHelper = function (e, ui) {
    ui.children().each(function () {
        $(this).width($(this).width());
    });
    return ui;
};
/*
   Ajax calling  
*/

function stateChanged() {
    if (xmlhttp.readyState == 4) {
        wait_till_complete(xmlhttp.readyState);
        if (xmlhttp.responseText == "") {
            if (receiveid == "search") {
                Clear_all();
            }
            document.getElementById("serror").innerHTML = "<center><span class='green'>No Recorrd Found.</span></center>";
            if(document.getElementById(receiveid))
            {
               document.getElementById(receiveid).innerHTML = "";
            }
            return;
        }
        try {
            if (receiveid == "employee") {
                document.getElementById(receiveid).innerHTML = fillHead(parse_json(xmlhttp.responseText));
                if (document.getElementById('salList')) {
                    $("#salList tbody").sortable({
                        helper: fixHelper,
                    });
                    $("#salList tbody").disableSelection();
                }
            }
            else if (receiveid == "SalarySheet") {
                document.getElementById(receiveid).innerHTML = fillHead(parse_json(xmlhttp.responseText));
                var _id = 0
                _id = get_id();
                if (_id != 0) {
                    getEmployee('e');
                } else {
                    getSheetName();
                }
                if (document.getElementById('salList')) {
                    $("#salList tbody").sortable({
                        helper: fixHelper,
                    });
                    $("#salList tbody").disableSelection();
                }
            }
            else if (receiveid == "salgrid") {
                fill_employee_SalaryHead(parse_json(xmlhttp.responseText));
            }
            else if (receiveid == "search") {
                fill_search(parse_json(xmlhttp.responseText));
                document.getElementById('serror').innerHTML = '';
            } else if (receiveid == "SheetName")
            {
                FillSheetnmae(parse_json(xmlhttp.responseText));
            }
            else if (receiveid == 'emp_search_list') {
                var fill = eval('(' + xmlhttp.responseText + ')');
                fill_EmployeeList(fill);
            }
            else if (receiveid == "edit") {
                var arr = xmlhttp.responseText.split("^");
                if (parseInt(arr[0]) > 0) {
                    edit(arr[0], arr[1]);
                }
                else {
                    closestatus();
                }
            }
            else if (receiveid == "delete") {
                var arr = xmlhttp.responseText.split("^");
                if (parseInt(arr[0]) > 0) {
                    delete_sheet(arr[0]);
                    closestatus();
                }
                else {
                    closestatus();
                }
            }
            else if (receiveid == "d_sheet") {
                var str = "";
                if (parseInt(xmlhttp.responseText) == 1) {
                    str = "<center><span class='red'>You can not delete this sheet because you already paid or you lock this sheet.</span></center>"
                }
                else if (parseInt(xmlhttp.responseText) == -1) {
                    str = "<center><span class='red'>Record in use.</span></center>";
                }
                else {
                    str = "<center><span class='red'>Record deleted successfully .</span></center>";
                    setTimeout("page_Search('s')", 1000);
                }
                document.getElementById("serror").innerHTML = str;
            } else if (receiveid == "PostingInAccountledger" || receiveid == "Delete_PostingAccountledger")
            {
                document.getElementById("serror").innerHTML = xmlhttp.responseText;
                setTimeout("page_Search('s')", 1000);
            } 
            else {
                document.getElementById(receiveid).innerHTML = xmlhttp.responseText;
            }
        } catch (e) {
            document.getElementById("serror").innerHTML = e.toString();
        }
        setTimeout("clearMsg();", 10000);
    }
    else {
        wait_till_complete(xmlhttp.readyState);
    }
}
function getSheetName() {
    ClearSheetname();
    senddata_post("SalaryProcess", "T=getSheet&SalaryPlan=" + document.getElementById('ddlEmpType').value + "&Period=" + document.getElementById('ddlPeriod').value, "SheetName")
}
function getSheetEmployee() {
    clearEmployee();
    if (document.getElementById("ddlSalarysheet")) {
        if(document.getElementById("ddlSalarysheet").value!=0)
        {
            senddata_post("SalaryProcess", "T=getemployee_sal&id=" + document.getElementById("ddlSalarysheet").value, "salgrid")
        }
    }
}
function FillSheetnmae(data) {
    ClearSheetname();
    if (data.length > 0) {
        for (var i = 0; i < data.length; i++) {
            var opt = document.createElement("option");
            opt.value = data[i].esid;
            opt.text = data[i].sheettitle;
            if (document.getElementById("ddlSalarysheet")) {
                document.getElementById("ddlSalarysheet").add(opt);
            }
        }
    }
}
function ClearSheetname() {
    if (document.getElementById("ddlSalarysheet")) {
        document.getElementById("ddlSalarysheet").innerHTML = "";
        var opt = document.createElement("option");
        opt.text = "--Select--";
        opt.value = 0;
        document.getElementById("ddlSalarysheet").add(opt);
    }
}

/*
  This function add employee to left pane 
*/
function fillEmployee(data) {
    var str = "";
    if (data.length > 0) {
        for (var i = 0; i < data.length; i++) {
            str += "<div class='block' id='" + data[i].AcName + "'><table cellpadding='2' cellspacing='0' id='staff" + i + "' width='100%' style='border-bottom:1px solid #000' class='r_i_font' >";
            str += "<tr>"
            str += "<td colspan='2' class='title2'><input type='checkbox' name='chkEmployee' onclick=\"chk('Employeselist','','child')\" id='chk" + i + "' />" + data[i].AcName + "</td><td style='text-align:right'>"
            str += "<a href=\"javascript:getEmployee_Sal_Detail('" + data[i].StaffId + "')\" style='background:url(images/addimg.png); height:16px; width:16px;'>Add</a>";
            str += "</td>";
            str += "</tr>"
            str += "<tr>"
            str += "<td class='r_i_font_l'>Staff Card Id. : </td><td colspan='2'>" + data[i].STAFFCARDID + "</td>";
            str += "</tr>";
            str += "<tr>"
            str += "<td class='r_i_font_l'>Department : </td><td colspan='2'><input type='hidden' name='staffid' id='staffid" + i + "' value='" + data[i].StaffId + "'/>" + data[i].GROUPNAME + "</td>";
            str += "</tr>";
            str += "<tr>";
            str += "<td class='r_i_font_l'>Designation : </td><td colspan='2' class='red'>" + data[i].DESIGTYPE + "</td>";
            str += "</tr>";
            str += "<tr>"
            str += "<td class='r_i_font_l'>Total Working Days : </td><td colspan='2'><span class='red' id='allowtoissue'></span></td>";
            str += "</tr>";
            str += "</table></div>";
        }
    } else { 
       str+="<center><span class='green'>No Recorrd Found.</span></center>"
    }
    return str;
}
/*
    this function remove master row and sub row also
*/
function search(val) {
    var div = document.getElementById('employee').getElementsByTagName('div');
    for (var i = 0; i < div.length; i++) {
        if (val != '') {
            if (div[i].id) {
                if (div[i].id.substring(0, val.length).toString().toUpperCase() == val.toString().toUpperCase()) {
                    div[i].setAttribute("class", "block");
                }
                else {
                    div[i].setAttribute("class", "none");
                }
            }
        }
        else {
            div[i].setAttribute("class", "block");
        }
    }
}
function del_m_row(ele) {
    var index = ele.parentNode.parentNode.parentNode.rowIndex;
    var ele = document.getElementById('salList');
    var staffid = 0;
    if (ele) {
        if (confirm("Are you sure you want to delete this employee from this sheet?")) {
            var master_Staff_Id = ele.rows[index].cells[2].getElementsByTagName("input");
            for (var i = 0; i < master_Staff_Id.length; i++) {
                if (master_Staff_Id[i].name == "staff_s_id") {
                    staffid = master_Staff_Id[i].value;
                }
            }
            var staff_ele = document.getElementsByName("staff_s_id");
            for (var i = parseInt(staff_ele.length) - 1; i >= 0; i--) {
                if (parseInt(staffid) == staff_ele[i].value) {
                    rem_row(parseInt(i) + 1)
                }
                correctSno();
            }

        }
    }
}
function rem_m_row(index) {
}
function correctSno() {
    var count = 1;
    var ele = document.getElementById('salList');
    if (ele) {
        var count = 1;
        for (var i = 0; i < ele.rows.length; i++) {
            if (ele.rows[i].getAttribute("row_type") == "m") {
                ele.rows[i].cells[1].innerHTML = count;
                count = parseInt(count) + 1;
            }
        }
    }
}
/*
   this function is used for insert sub row of master row
*/
function ins_row(index,staffid) {
    var ele = document.getElementById('salList');
    var row = ele.insertRow(parseFloat(index) + 1);
    var sal_type = ""; 
    if (ele.rows[index].hasAttribute("saltype")) {
        sal_type = ele.rows[index].getAttribute("saltype");
    }
    row.setAttribute("row_type", "c")
    row.setAttribute("saltype", sal_type)
    var cell = row.insertCell(0);
    cell.innerHTML = "<span class='float'><img src='Images/refresh.png' height='10px' title='Clear manual value, and calculate salary according to formula.'  onclick='cl(this.parentNode.parentNode.parentNode.rowIndex);'/></span><span class='float'></span><a href='javascript:void(0)' title='Remove this row for this employee.' onclick='rem_row(this.parentNode.parentNode.rowIndex);'><img src='Images/n1.jpg' style='margin-left:50px;margin-top:10px' height='10px' width='10px' alt='Add'/></a>"
    cell = row.insertCell(1);
    cell = row.insertCell(2);
    cell.innerHTML = "<input type='hidden' id='staff_s_id' name='staff_s_id' value='" + staffid + "' /><input type='checkbox' onclick='fill_Total();' name='include' title='Include in salary' checked='checked' />"
    cell = row.insertCell(3);
    cell.innerHTML = "";
    cell = row.insertCell(4);
    cell.innerHTML = "<input type='text' name='row_head' />"
    cell = row.insertCell(5);
    cell.setAttribute("onclick", "s_col(this);");
    cell.innerHTML = "<input type='text' datatype='int' id='twd" + index + (parseInt(index) + 1) + "' onchange=\"check_value(this,'d');\" defaultvalue='0' name='twd' value='0' />";
    for (var j = 6; j < ele.rows[0].cells.length; j++) {
        cell = row.insertCell(j);
        cell.setAttribute("onclick", "s_col(this);");
        cell.innerHTML = "<input type='text' datatype='int' defaultvalue='0' onclick='s_col(this);' manual='N' value='0'  maxlength='20' id='salHead" + index + j + "' onkeypress='return numbersonly(this,event);' onkeyup='change_manual(this);' onchange=\"check_value(this,'s')\" name='salHead' value='' />";
    }
}

/*
   used for remove sub row directly or remove master row indirectly means used in  rem_m_row.
*/
function rem_row(index) {
    var ele = document.getElementById('salList');
    ele.deleteRow(index);
    fill_Total();
}
/*
    used for clear manual value entered by the user in formula column
*/
function cl(rwindex) {
    var ele = document.getElementById('salList');
    for (var i = 0; i < ele.rows[rwindex].cells.length; i++) {
        var inp = ele.rows[rwindex].cells[i].getElementsByTagName('input');
        for (j = 0; j < inp.length; j++) {
            if (inp[j].getAttribute('manual')) {
                inp[j].setAttribute('manual', "N");
            }
        }
        calculate_formula(rwindex);
    }
}
/*
   used for add employee salary sheet.
*/
function fill_employee_SalaryHead(data) {
    var tbl_ele = document.getElementById('salList');
    var colCount = 0
    if (tbl_ele) {
        colCount=parseInt(tbl_ele.rows[0].cells.length)-5;
        for (var i = 0; i < data.length; i++) {
            var staff_s_id = document.getElementsByName("staff_s_id");
            var count = 0;
            for (var k = 0; k < staff_s_id.length; k++) {
                if (parseInt(i) == 0 || data[i].StaffId != data[i - 1].StaffId) {
                    if (parseInt(staff_s_id[k].value) == data[i].StaffId) {
                        count = 1; //check weather it exists or not.
                    }
                }
            }
            if (count == 0) {
                if (parseInt(i) == 0 || data[i].StaffId != data[i - 1].StaffId || data[i].ROW_TYPE != data[i - 1].ROW_TYPE || ((parseInt(i) % parseInt(colCount)) == 0 && data[i].ROW_TYPE.toString().toUpperCase() == 'C')) {
              //  if ((parseInt(i) % parseInt(colCount)) == 0 || data[i].StaffId != data[i - 1].StaffId) {
                    var row_index = parseInt(tbl_ele.rows.length) - 1;
                     if (document.getElementById('total_row')) {
                        row_index = parseInt(tbl_ele.rows.length) - 2;
                    }
                    var row = tbl_ele.insertRow(row_index);
                    row.setAttribute("row_type", data[i].ROW_TYPE)
                    row.setAttribute("saltype", data[i].SalType)
                    if (data[i].ROW_TYPE.toString().toUpperCase() == "M") {
                        var cell = row.insertCell(0);
                        cell.innerHTML = "<span class='float'><img src='Images/refresh.png' height='10px' title='Clear manual value, and calculate salary according to formula.'  onclick='cl(this.parentNode.parentNode.parentNode.rowIndex);'/></span><span class='float'><a href='javascript:void(0)' onclick='javascript:del_m_row(this);'  class='delete'></a></span><img onclick='ins_row(this.parentNode.parentNode.rowIndex," + data[i].StaffId + ");' title='Add new row for this employee.' src='Images/p1.jpg' style='margin-left:50px;margin-top:10px' height='10px' width='10px' alt='Add'/>"
                        cell = row.insertCell(1);
                        cell.innerHTML = row_index;
                        cell = row.insertCell(2);
                        cell.innerHTML = "<input type='hidden' id='staff_s_id' name='staff_s_id' value='" + data[i].StaffId + "' />" + data[i].AcName;
                        cell = row.insertCell(3);
                        cell.innerHTML = data[i].UnderGrade;
                        cell = row.insertCell(4);
                        cell.innerHTML = data[i].DesigType + "<input type='hidden' name='row_header' value='' />"
                        cell = row.insertCell(5);
                        cell.setAttribute("onclick", "s_col(this);");
                        cell.innerHTML = "<input type='text' datatype='int' id='twd" + i + "' onchange=\"check_value(this,'d');\" defaultvalue='" + data[i].Total_Work_day + "' name='twd' value='" + data[i].Total_Work_day + "' />";
                    } else {
                        var cell = row.insertCell(0);
                        cell.innerHTML = "<span class='float'><img src='Images/refresh.png' height='10px' title='Clear manual value, and calculate salary according to formula.'  onclick='cl(this.parentNode.parentNode.parentNode.rowIndex);'/></span><span class='float'></span><img title='Remove this row for this employee.' onclick='javascript:rem_row(this.parentNode.parentNode.rowIndex);' src='Images/n1.jpg' style='margin-left:50px;margin-top:10px' height='10px' width='10px' alt='delete'/>"
                        cell = row.insertCell(1);
                        cell.innerHTML = "";
                        cell = row.insertCell(2);
                        var check = "";
                        if (data[i].include) {
                            check = "checked='checked'";
                        }
                        cell.innerHTML = "<input type='hidden' id='staff_s_id' name='staff_s_id' value='" + data[i].StaffId + "' /><input type='checkbox' onclick='fill_Total();' name='include' title='Include in salary' " + check + " />"
                        cell = row.insertCell(3);
                        cell.innerHTML = "";
                        cell = row.insertCell(4);
                        cell.innerHTML = "<input type='text' name='row_head' value='" + data[i].row_head + "' />"
                        cell = row.insertCell(5);
                        cell.setAttribute("onclick", "s_col(this);");
                        cell.innerHTML = "<input type='text' datatype='int' id='twd" + i + "' onchange=\"check_value(this,'d');\" defaultvalue='" + data[i].Total_Work_day + "' onclick='s_col(this);' name='twd' value='" + data[i].Total_Work_day + "' />";
                    }
                    for (var j = 6; j < tbl_ele.rows[0].cells.length; j++) {
                        cell = row.insertCell(j);
                        cell.setAttribute("onclick", "s_col(this);");
                        cell.innerHTML = "<input type='text' defaultvalue='0' onclick='txt_onClick(event);' datatype='int' manual='N' value='0'  maxlength='20' id='salHead" + i + "'  onkeypress='return numbersonly(this,event);' onkeyup='change_manual(this);' onchange=\"check_value(this,'s')\" name='salHead' value='' />";
                    }
                }
                var count = 0;
                for (var j = 5; j < tbl_ele.rows[0].cells.length; j++) {
                  //  alert("this is cell id=" + tbl_ele.rows[0].cells[j].id + "this is sal id=" + data[i].SalId + "this is amount=" + data[i].salamount + "this is row index=" + row_index);
                    if (parseInt(tbl_ele.rows[0].cells[j].id) == parseInt(data[i].SalId)) {
                       // var row_index = parseInt(tbl_ele.rows.length) - 2;
                        tbl_ele.rows[row_index].cells[j].innerHTML = "<input type='text' datatype='int' onclick='txt_onClick(event);' manual='N' maxlength='20'  onkeypress='return numbersonly(this,event);'  onkeyup='change_manual(this);'  id='salHead" + i + "' name='salHead'  onchange=\"check_value(this,'s')\" defaultvalue='" + data[i].salamount + "' value='" + data[i].salamount + "' />"
                    }
                }
            }
            Remove_Emp_from_List(data[i].StaffId);
        }
    }
    if (document.getElementById('salgrid')) {
        document.getElementById('salgrid').innerHTML = "";
    }
    if (tbl_ele) {
        CreateTotRow();
        if (parseInt(get_id()) == 0) {
            // refresh_all();
            fill_Total();
        }
    } else {
        alert("Please select salary plan first!");
        document.getElementById('serror').innerHTML = '';
    }
    if (parseInt(get_id()) != 0) {
        get_Salary_sheet_value();
        correctSno();
    }
}
/*
   calculate all value acc to formula.
*/
function refresh_all() {
    var tbl_ele = document.getElementById('salList');
      if (tbl_ele) {
          for (var i = 1; i < (parseInt(tbl_ele.rows.length)-2); i++) {
              cl(i);
          }
      }
  }
  /*
    used for add salary head in salary sheet
  */
function fillHead(data) {
    var str = "";
    var ddoString = "";
    var count = 0;
    var day = 0
    var strCol = [];
    var strFormula = [];
    if (document.getElementById("ddlMonth")) {
        var _date = document.getElementById("ddlMonth").value;
        if (_date != "") {
            var _dayArr = _date.split("/");
            day = _dayArr[0];
        }
    }
    if (data.length > 0) {
        ddoString += "<select id='d_s_saltitle'  onchange='setDefaultValue();refresh_all();' name='d_s_saltitle'><option value='0'>--Select--</option>"
        for (var i = 0; i < data.length; i++) {
            if (parseInt(data[i].inc) == 1) {
                ddoString += "<option value='" + data[i].SalId + "'>" + data[i].SalTitle + "</option>"
            }
        }
        ddoString += "</select>"
        document.getElementById('ddo').innerHTML = ddoString;
        str += "<table id='salList' cellpadding='0' cellspacing='0' border='1' style='border-collapse:collapse;' class='tbElist'>"
        str += "<tr>"
        str += "<th>Action</th><th>S.No.</th><th id='staffid'><div class='d_head_Name' >Employee Name</div></th><th>Scale</th><th id='row_head'>Designation</th><th id='days' total='" + day + "'>Days</br>(" + day + ")</th>"
        for (var i = 0; i < data.length; i++) {
            if (parseInt(data[i].inc) == 1) {
                str += "<th  id='" + data[i].SalId + "' formula='" + data[i].Formula + "' a_title='" + data[i].SalTitle + "'><div class='d_head' title=" + data[i].Formula + ">" + data[i].SalDisplayName
                strCol.push(data[i].SalId);
                strFormula.push(data[i].Formula);
                if (data[i].Formula != "") {
                    str += "</br>"
                }
                //str += "</br>(" + data[i].Formula + ")";
                str+="</div></th>";
                count += 1;
            }
        }
        document.getElementById('colname').value = strCol;
        document.getElementById('formula').value = strFormula;
        document.getElementById('formula_span').value = strFormula;
        str += "</tr>"
//        for (var i = 0; i < data.length; i++) {
//            if (parseInt(data[i].inc) == 1) {
//                str += "<td>&nbsp;</td>";
//            }
//        }
        str += "<tr><td id='salgrid' colospan='" + data.length + "'></td></tr></table>"
    } else {
        str = "<center><span class='green'>No Record Found.</span></center>"
    }
    if (count == 0) {
        str = "<center><span class='green'>No Record Found.</span></center>"
    }
    return str;
}
/*Sortable grid*/


function getEmployee_Sal_Detail(_empval) {
    if (_empval != "") {
        senddata_post("SalaryProcess", "T=getemployee_sal&staffid=" + _empval, "salgrid")
    }
}
function clear_TimeOut() {
    var interval_id = window.setInterval("", 9999); // Get a reference to the last
    // interval +1
    for (var i = 1; i < interval_id; i++) {
        window.clearInterval(i);
    }
}
/*Add Employee In The List*/
function addEmployeeList() {
    $("#EmplyeeList").fadeIn("slow", 1000);
    $("#blank").fadeIn("slow", 1000);
}
function closeEmployeeList() {
    $("#EmplyeeList").fadeOut("slow", 1000);
    $("#blank").fadeOut("slow", 1000);
}
function chng_searchfor(ele, chele) {
if (parseInt(ele.value) != 3) {
    document.getElementById(chele).innerHTML = ele.options[ele.options.selectedIndex].text + ' :';
}
else {
    document.getElementById(chele).innerHTML="Employee Id :"
}
}
/* used for get salary detail selected by user to add in salary sheet*/

function get_all_Employee_Sal_Detail() { 
    var chk_Employee = document.getElementsByName('chkEmployee');
    var staff_id = document.getElementsByName('staffid');
    var div = document.getElementById('employee').getElementsByTagName("div"); 
    var _empval = "";
    for (var i = 0; i < chk_Employee.length; i++) {
        if (chk_Employee[i].checked && div[i].getAttribute("class").toString().toUpperCase()=="BLOCK") {
            _empval += staff_id[i].value+",";
        }
    }
    if (_empval != "") {
        senddata_post("SalaryProcess", "T=getemployee_sal&staffid=" + _empval.substr(0,_empval.length-1), "salgrid")
    }
}
/*return total no. of days that is displayed in header row */
function getDays() {
    var eleTable = document.getElementById('salList');
    var tot_day =0;
    if (eleTable) {
        if (!(isNaN(eleTable.rows[0].cells[5].getAttribute("total")))) {
            tot_day = eleTable.rows[0].cells[5].getAttribute("total");
        }
    }
    return tot_day;
}
/*
   used for check the text box that if the value of textbox is blank than fill default value.
*/ 
function check_value(ele, type) {
    if (ele) {
        var val = ele.getAttribute('defaultvalue');
        var manual = ele.getAttribute('manual');
        tot_day = getDays();
        if (ele.value == "") {
            ele.value = val;
        } else if (type.toString().toUpperCase() == 'D') {
            if (parseFloat(ele.value) > parseFloat(tot_day)) {
            ele.value = val;
             }
        }else {
           if(manual.toString().toUpperCase()=="N") {
                ele.value = val;
           }
        }
        calculate_formula(ele.parentNode.parentNode.rowIndex);
    }
}
/*
  change textbox manual attribute to yes
*/
function change_manual(ele) {
    ele.setAttribute("manual", "Y");
    //calculate_formula(ele.parentNode.parentNode.rowIndex);
}
/*
   fill default value in the textbox that user leave blank
*/
function setDefaultValue() {
    var tbl_ele = document.getElementById('salList');
    if (tbl_ele) {
        for (var i = 1; i < tbl_ele.rows.length; i++) {
            for (var j = 0; j < tbl_ele.rows[i].cells.length; j++) {
                var _inp_ele = tbl_ele.rows[i].cells[j].getElementsByTagName('input')[0];
                if (_inp_ele) {
                    var defaultValue = 0;
                    defaultValue = _inp_ele.getAttribute("defaultvalue");
                    if (!isNaN(defaultValue)) {
                        _inp_ele.value = defaultValue;
                    }
                }
            }
        }
    }
}
/*
    formula calculation

*/
function calculate_formula(index) {
    var ele = document.getElementById('salList');
    if (ele) {
        var _title = [];
        var _value = [];
        for (var i = 6; i < ele.rows[index].cells.length; i++) {
            var inp = ele.rows[index].cells[i].getElementsByTagName("input");
            var title = ele.rows[0].cells[i].getAttribute("a_title");
            var formula = ele.rows[0].cells[i].getAttribute("formula");
            if (formula != "") {
                var tempform = split_f_val(formula);
                for (var k = 0; k < tempform.length; k++) {
                    var _found = 0;
                    if (isNaN(tempform[k])) {
                        if (tempform[k] != "+" && tempform[k] != "-" && tempform[k] != "%" && tempform[k] != "/" && tempform[k] != "*") {
                            for (var j = 0; j < _title.length; j++) {
                                var _vtemp = tempform[k];
                                if (_vtemp.toString().toLowerCase() == _title[j].toLowerCase()) {
                                    if (typeof (_value[j]) == "undefined") {
                                        tempform[k] = 0;
                                    }
                                    else {
                                        tempform[k] = _value[j];
                                        _found = 1;
                                    }
                                }
                            }
                            if (parseInt(_found)==0) {
                                tempform[k] = 0;
                            }
                        }
                    }
                }
                Formula = "";
                for (var l = 0; l < tempform.length; l++) {
                    Formula += tempform[l];
                }
                Formula = Formula.replace("%", "*.01*");
                //  alert(Formula);
                var totFormula = eval(Formula);
                if (!isNaN(totFormula)) {
                    _title.push(title);
                    var total=0;
                    total = roundValue(totFormula, getRoundUpValue());
                    if (!isNaN(total)) {
                        if (inp[0].getAttribute('manual').toString().toUpperCase() == "N") {
                            inp[0].value = total;
                            _value.push(total);
                        }
                        else {
                            total = Calculate_InitialAmount(ele, index, total, i);
                            _value.push(parseFloat(inp[0].value));
                        }
                    }
                }
            }
            else {
                _title.push(title);
                //                if (i 5 && inp[0].value != 0) {
                var amount = inp[0].getAttribute("defaultvalue");
                if (inp[0].getAttribute('manual').toString().toUpperCase() == "Y") {
                   amount=parseFloat(inp[0].value);
               }
               var _val = Calculate_InitialAmount(ele, index, amount, i)
                    _value.push(parseFloat(_val));
                    inp[0].value =parseFloat(_val);
               // }
                //else {
                  //  _value.push(inp[0].value);
                //}
            }
        }
        }
        fill_Total();
}
/*
   used for calculate the salary according to days 
*/
function Calculate_InitialAmount(ele, index, amount,i) {
    var tot_day = 0;
    var actual_day = 0;
    var a_amount = 0;
    var cal_amount = 0
    tot_day = getDays();
    if (!isNaN(ele.rows[index].cells[4].getElementsByTagName('input')[0].value)) {
        actual_day = ele.rows[index].cells[4].getElementsByTagName('input')[0].value;
    }
    var s_title = 0
    if (document.getElementById("d_s_saltitle")) {
        s_title = document.getElementById("d_s_saltitle").value;
    }
    if (parseInt(s_title) != 0) {
        var _found = 0;
        if (ele.rows[0].cells[i].id == parseInt(s_title)) {
                var ele_input=ele.rows[index].cells[i].getElementsByTagName("input")[0];
                if (ele_input) {
                    if (ele_input.getAttribute("manual") == "N") {
                        a_amount = amount;
                        if (parseInt(tot_day) != 0) {
                            cal_amount = (a_amount / tot_day) * actual_day;
                        }
                        ele_input.value = roundValue(cal_amount, getRoundUpValue());
                        _found = 1;
                    }
                    else {
                        cal_amount = ele_input.value;
                        _found = 1;
                    }
                }
            }
        if (_found == 1) {
            return roundValue(cal_amount, getRoundUpValue());   
        }
        else {
            return amount;
        }
    }
    else {
//        a_amount = amount;
//        cal_amount = (a_amount / tot_day) * actual_day;
        // inp[0].value = roundValue(cal_amount);
        return amount;
    }
}
function Save_Salary() {
    var title = "";
    if (document.getElementById('txtSheetTitle').value != "") {
        title = document.getElementById('txtSheetTitle').value;
    } else {
        title = prompt("Please enter sheet name for this sheet.", "");
        if (!(title)) {
            return;
        }
        else {
            document.getElementById('txtSheetTitle').value = title;
        }
    }
    var date = document.getElementById('sDate').value;
    var ele = document.getElementById("salList");
    var strSave = "id=" + get_id() + "&title=" + title + "&date=" + date + "&colname=" + document.getElementById('colname').value + "&formula=''&month=" + document.getElementById('hdnMonth').value + "&Empid=" + document.getElementById('hdnEmpType').value + '&Period=' + document.getElementById("hdnPeriod").value;
    if (ele) {
        for (var i = 1; i < ele.rows.length; i++) {
            var row_type = "";
            var sal_type = "";
            var include = "";
            if(ele.rows[i].hasAttribute("row_type"))
            {
               row_type = ele.rows[i].getAttribute("row_type");
           }
           if (ele.rows[i].hasAttribute("saltype")) {
               sal_type = ele.rows[i].getAttribute("saltype");
           }
           strSave += "&row_type=" + row_type + "&saltype=" + sal_type;
           var found = 0;
            for (var j = 0; j < ele.rows[i].cells.length; j++) {
                var inp = ele.rows[i].cells[j].getElementsByTagName('input');
                if (inp[0]) {
                    strSave += "&"+ele.rows[0].cells[j].id + "=" + inp[0].value;
                }
                if (row_type == "m") {
                    include = 1;
                    found = 1;
                }
                else {
                    if (inp[1]) {
                        if (inp[1].type == 'checkbox' && inp[1].name == 'include') {
                            if (inp[1].checked) {
                                include=1
                                found = 1;
                            }
                        }
                    }
                }
            }
            if (found == 0) {
                include=0
            }
            strSave += "&include=" + include;
        }
    }
    if (strSave != "") {
        senddata_post("SalaryProcess", "T=SaveSalary&" + strSave, "serror");
    }
}
function page_Search(val) {
    var q = "";
    var fyyear = 0;
    var salaryplan = 0;
    var month = 0;
    if (document.getElementById('back').className == 'none') {
        if (document.getElementById('ddlEmpType')) {
            document.getElementById('ddlEmpType').value = '0';
        }
        if (document.getElementById('ddlMonth')) {
            document.getElementById('ddlMonth').value = '0';
        }
    }
    if (document.getElementById('txtfind')) {
        q = document.getElementById('txtfind').value;
    }
    if (document.getElementById('ddlFinancialyear')) {
        fyyear = document.getElementById('ddlFinancialyear').value;
    }
    if (document.getElementById('ddlMonth')) {
        if (document.getElementById('ddlMonth').value!="") {
            month = document.getElementById('ddlMonth').value;
        }
    }
    if (document.getElementById('ddlEmpType')) {
        if (document.getElementById('ddlEmpType').value != "") {
           salaryplan = document.getElementById('ddlEmpType').value;
        }
    } 
    var _currentPage = 1;
    var maxRecord = 20;
    if (val != "p") {
        if (document.getElementById('_currentPage')) {
            document.getElementById('_currentPage').value = 1;
        }
    }
        if (document.getElementById('_currentPage')) {
            _currentPage = document.getElementById('_currentPage').value;
        }
        senddata_post("SalaryProcess", "T=search&q=" + q + "&page=" + _currentPage + "&maxrecord=" + maxRecord+"&month="+month+"&salplan="+salaryplan+"&fyyear="+fyyear, "search");
        hide("none", "block");
        if (document.getElementById('dispaly_formual')) {
            document.getElementById('dispaly_formual').value ="";
        }
       
}
function fill_search(data) {
    var strSheet = "";
    if (data.length > 0) {
        strSheet = "<table id='search_grid' cellpadding='0' cellspacing='0' class='tblist' width='100%'>"
        strSheet += "<tr><th><input type='checkbox' id='check_all' name='check_all' onclick='chk(\"search_grid\", this.id, \"master\")' /></th><th>S.No.</th><th>Sheet Name</th><th>Month</th><th>Salary Plan</th><th>Processing Date</th><th>Actual Date</th><th>Posted In Account Ledger</th><th>Email</th><th>SMS</th><th>Status</th><th>Edit</th><th>Delete</th><th>Create Excel File</th></tr>"
        
        for (var i = 0; i < data.length; i++) {
            var cls = "";
            if (parseInt(i) % 2 != 0) {
              cls="class='alt'"
          }
          paging({ "totRecord": "" + data[i].maxcount + "", "fnName": "page_Search('p');", "maxShowRecord": "" + data[i].maxrecord + "", "noOfLink": "5", "id": ["Paging"] });
          strSheet += "<tr>";
          strSheet += "<td " + cls + "><input type='checkbox' value='" + data[i].esid + "' onclick='chk(\"search_grid\",\"\", \"child\")' id='check" + i + "' name='check' /></td>";
            strSheet += "<td " + cls + ">" + data[i].CNT + "</td>";
            strSheet += "<td "+cls+"><div>"+data[i].sheettitle+"</div></td>";
            strSheet += "<td id='" + data[i].monthno + "'" + cls + ">" + data[i].month + "</td>";
            strSheet += "<td id='" + data[i].salplanid + "'" + cls + ">" + data[i].SalPlanName + "</td>";
            strSheet += "<td " + cls + ">" + data[i].processingdate + "</td>";
            strSheet += "<td " + cls + ">" + data[i].actualdate + "</td>";
            strSheet += "<td " + cls + ">";
            if(data[i].postinginaccountledger)
            {
                strSheet += "<img alt='Fail' src='Images/Yes.png' height='20px' width='20px' />";
            }else{
                strSheet += "<img alt='Success' src='Images/no.png' height='20px' width='20px' />";
            }
            strSheet+="</td>";
            strSheet += "<td " + cls + ">";
            if (data[i].send_email) {
                strSheet += "<img alt='Fail' src='Images/Yes.png' height='20px' width='20px' />";
            } else {
                strSheet += "<img alt='Success' src='Images/no.png' height='20px' width='20px' />";
            }
            strSheet += "</td>";
            strSheet += "<td " + cls + ">";
            if (data[i].send_sms) {
                strSheet += "<img alt='Fail' src='Images/Yes.png' height='20px' width='20px' />";
            } else {
                strSheet += "<img alt='Success' src='Images/no.png' height='20px' width='20px' />";
            }
            strSheet += "</td>";
            if (parseInt(data[i].status) == 1) {
                strSheet += "<td " + cls + " id='status" + i + "'><a href='javascript:void(0)' onclick='changestatus(" + data[i].esid + ",\"status" + i + "\",\"0\",\"status\")' >Unlock It</a></td>";
                strSheet += "<td " + cls + " id='edit_rec" + i + "'><a href='javascript:void(0)' onclick='changestatus(" + data[i].esid + ",\"edit_rec" + i + "\",this.parentNode.parentNode.rowIndex,\"edit\")' >Edit</a></td>";
                strSheet += "<td " + cls + " id='delete_rec" + i + "'><a href='javascript:void(0)' onclick='changestatus(" + data[i].esid + ",\"delete_rec" + i + "\",this.parentNode.parentNode.rowIndex,\"delete\")' >Delete</a></td>";
            }
            else {
                strSheet += "<td " + cls + " id='status" + i + "'><a href='javascript:void(0)' onclick='changestatus(" + data[i].esid + ",\"status" + i + "\",\"1\",\"status\")' >Lock It</a></td>";
                strSheet += "<td " + cls + " id='edit_rec" + i + "'><a href='javascript:void(0)' onclick='edit(" + data[i].esid + ",this.parentNode.parentNode.rowIndex)' >Edit</a></td>";
                strSheet += "<td " + cls + " id='delete_rec" + i + "'><a href='javascript:void(0)' onclick='delete_sheet(" + data[i].esid + ")' >Delete</a></td>";
            }
            strSheet += "<td " + cls + " id='excel" + i + "'><a href='javascript:void(0)' onclick='CreateExcel(" + data[i].esid + ",\"excel" + i + "\")' >Create Excel</a></td>";
            strSheet += "</tr>";
        }
    }
    else {
        strSheet = "<center><span class='green'>No Record Found.</span></center>"
    }
    document.getElementById('SalarySheet').innerHTML = strSheet;
}
function delete_sheet(id) {
    if (confirm('Are you sure you want to delete this sheet?')) {
        senddata_post('SalaryProcess', 'T=delete&id=' + id, 'd_sheet');
    }
}
function edit(id, index) {
    // back_page();
    var ele = document.getElementById("search_grid")
    if (ele) {
        document.getElementById('emp_sal_id').value = id;
        document.getElementById('txtSheetTitle').value = ele.rows[index].cells[2].getElementsByTagName('div')[0].innerHTML;
        document.getElementById('sDate').value = ele.rows[index].cells[5].innerHTML;
        var ele_option = document.getElementById('ddlMonth').options;
        for (var i = 0; i < ele_option.length; i++) {
            if (ele_option[i].innerHTML.toString().toLowerCase() == ele.rows[index].cells[3].innerHTML.toString().toLowerCase()) {
                ele_option[i].selected = true;
            }
        }
       // document.getElementById('ddlMonth').value = ele.rows[index].cells[2].id;
        document.getElementById('ddlEmpType').value = ele.rows[index].cells[4].id;
        senddata_post('SalaryProcess', 'T=edit&Empid=0&Period=0&id=' + id, 'SalarySheet');
        hide("block", "none");
    }
}
function get_id() {
    var _id = 0
    var ele_ed_id = document.getElementById('emp_sal_id');
    if (ele_ed_id) {
        _id = document.getElementById('emp_sal_id').value;
    }
    return _id;
}
function Cal_Total(cell_index) {
    var ele = document.getElementById('salList');
    if (ele) {
        var total = 0;
        var row_len = parseInt(ele.rows.length) - 2;
        if (document.getElementById('total_row')) {
            for (var j = 1; j < row_len; j++) {
                var include_ele = ele.rows[j].cells[2].getElementsByTagName('input')[1];
                var incl = 1;
                if (include_ele) {
                    if (include_ele.type == 'checkbox' && include_ele.name == "include") {
                        if (!(include_ele.checked)) {
                            incl = 0;
                        }
                    }
                }
                if (parseInt(incl) == 1) {
                    if (ele.rows[j].cells[cell_index]) {
                        var inp = ele.rows[j].cells[cell_index].getElementsByTagName('input')[0];
                        if (inp) {
                            if (!isNaN(inp.value)) {
                                total += parseFloat(inp.value)
                            }
                        }
                    }
                }
            }
            ele.rows[row_len].cells[cell_index].innerHTML = "<b>" + roundValue(total, getRoundUpValue()) + "</b>"
        }
    }
}
function CreateTotRow() {
    if (!(document.getElementById("total_row"))) {
        var ele = document.getElementById('salList');
        if (ele) {
            var row_len = ele.rows.length;
            var cell_len = ele.rows[0].cells.length;
            var n_row = ele.insertRow(parseInt(row_len) - 1);
            n_row.id = "total_row";
            for (var i = 0; i < cell_len; i++) {
                var cell = n_row.insertCell(i);
                cell.setAttribute("style", "text-align:right");
                if (parseInt(i) == 4) {
                    cell.innerHTML = "<b>Total</b>";
                }
                else {
                    cell.innerHTML = "";
                }
            }
        }
        fill_Total();
    }
}
function fill_Total() {
    var ele = document.getElementById('salList');
    for (i = 5; i < ele.rows[0].cells.length; i++) {
        Cal_Total(i);
    }
}
function clearMsg() {
    document.getElementById('serror').innerHTML = "";
}
function getRoundUpValue()
{
   return document.getElementById('round').value;
}
function Clear_all() {
    document.getElementById('txtSheetTitle').value = '';
    document.getElementById('d_s_saltitle').value = '0';
  //  document.getElementById('copyfrom').className = "none";
    if (document.getElementById('back').className == 'none') {
        document.getElementById('ddlMonth').value = '0';
        document.getElementById('ddlEmpType').value = '0';
        //document.getElementById('copyfrom').className = "";
    }
    document.getElementById('SalarySheet').innerHTML = '';
    document.getElementById('Paging').innerHTML = '';
    document.getElementById('dispaly_formual').value = '';
}
function clearSheet() {
    if (document.getElementById('SalarySheet')) {
        document.getElementById('SalarySheet').innerHTML = '';
    }
}
function clearEmployee() {
    var ele_tab = document.getElementById('salList');
    if (ele_tab) {
        if (ele_tab.rows.length > 1) {
            for (var i = ele_tab.rows.length - 2; i > 0 ; i--) {
                rem_row(i);
            }
        }
    }
}
function txt_onClick(e) {
    //if (e.stopPropagation) {
    //    e.stopPropagation();
    //}
    //else {e.cancelBubble = true; }
}
function prevent_default(e) {
    e.stopPropagation();
    // now do your stuff        
}
function s_col(ele) {
  var tbl_ele = document.getElementById('salList');
  if (tbl_ele) {
      for (var i = 1; i < tbl_ele.rows.length; i++) {
          for (var j = 0; j < tbl_ele.rows[i].cells.length; j++) {
              tbl_ele.rows[i].cells[j].setAttribute("style", "")
          }
      }
      var index = 0;
      if (ele.cellIndex) {
          index = ele.cellIndex;
      } else if (ele.parentNode.cellIndex) {
          index = ele.parentNode.cellIndex;
      }
      ele.setAttribute("style", "border:1px solid red")
      if (document.getElementById('dispaly_formual')) {
          document.getElementById('dispaly_formual').value = tbl_ele.rows[0].cells[index].getAttribute("formula");
      }
      if (ele.getElementsByTagName('input')[0] || ele.type=='text') {
          var _eleInput = ele.getElementsByTagName('input')[0];
         var elemLen = _eleInput.value.length;
          if (document.selection) {
              // Set focus
              _eleInput.focus();
              // Use IE Ranges
              var oSel = document.selection.createRange();
              // Reset position to 0 & then set at end
              oSel.moveStart('character', -elemLen);
              oSel.moveStart('character', elemLen);
              oSel.moveEnd('character', 0);
              oSel.select();
          }
          else if (_eleInput.selectionStart || _eleInput.selectionStart == '0') {
              // Firefox/Chrome
              _eleInput.selectionStart = elemLen;
              _eleInput.selectionEnd = elemLen;
              _eleInput.focus();
          } // if
      }
  }
}
/* Get Employee for add in salary sheet*/
function getEmployee_for_adding() {
    senddata_post("SalaryProcess", "T=Employee&Empid=" + document.getElementById('ddlEmployeelistType').value + "&DesigId=" + document.getElementById('ddlDesignatoin').value + '&groupid=' + document.getElementById('ddlDeparment').value + '&searchfor=' + document.getElementById('ddlSearchFor').value + '&searchval=' + document.getElementById('txtSearch').value, "emp_search_list")
}
function fill_EmployeeList(data) {
    if (data.length > 0) {
        var str = "";
        //str+="<table id='employee_search_list' cellpadding='0' cellspacing='0' class='tblist' width='100%'><tr><th><input type='checkbox' id='chkAll' onclick='chk(\"emp_search_list\",this.id,\"master\")'/></th><th>Staff Image</th><th>Employee Name-(Emplyee Id)</th><th>Designation</th><th>Department</th><th>Employee Type</th><th>Action</th></tr>"
        for (var i = 0; i < data.length; i++) {
            var staff_s_id = document.getElementsByName("staff_s_id");
            var count = 0;
            for (var k = 0; k < staff_s_id.length; k++) {
                    if (parseInt(staff_s_id[k].value) == data[i].StaffId) {
                        count = 1; //check weather it exists or not.
                    }
                }
                if (count == 0) {
                    var cls = '';
                    if (parseInt(i) % 2 == 0) {
                        cls = "alt";
                    }
                    str += "<div class='emp_s_list'><div class='left_emp_s_list'><div><input type='checkbox' name='chkEmployee' id='chkChild" + data[i].StaffId + "' onclick='chk(\"emp_list\",\"\",\"child\")' value='" + data[i].StaffId + "' /></div>"
                    if (data[i].StaffImage != '') {
                        str += "<div style='height:40px; width:40px;border:1px solid #000'><img src='StaffImage/" + data[i].StaffImage + "' height='40px' width='40px' alt=''/></div>"
                    }
                    else {
                        str += "<div style='height:40px; width:40px;border:1px solid #000'><img src='Images/SUPERVISOR.png' height='40px' width='40px' alt=''/></div>"
                    }
                    str += "</div><div class='right_emp_s_list'><div id='title'>" + data[i].AcName + "&nbsp;&nbsp;(" + data[i].STAFFREGID + ")</div><div><a href='javascript:void(0)' onclick='AddEmployee(" + data[i].StaffId + ")'>Add</a></div>";
                    str += "<div class='clear'></div><div>Designation :</div><div>" + data[i].DESIGTYPE + "</div><div class='clear'></div><div>Department :</div><div>" + data[i].GROUPNAME + "</div><div class='clear'></div><div>Employee Type :</div><div>" + data[i].EMPTYPE + "</div>";
                    str += "</div>"
                    str += "</div></div>";
                }
        }
       // str += "</div>";
        document.getElementById(receiveid).innerHTML = str;
    }
    else {
       document.getElementById(receiveid).innerHTML='<center><span class="green">No Record Found.</span></center>'
    }

}
function AddEmployee(id) {
    var _sid = new Array();
    if (parseInt(id) == 0) {
        var eleTab = document.getElementsByName('chkEmployee');
        for (var i = 0; i < eleTab.length; i++) {
            if (eleTab[i].checked) {
                _sid.push(eleTab[i].value);
            }
        }
    } else {
    _sid.push(id);
}
  var tbl_ele = document.getElementById('salList');
  var _tot_rec = 0;
    if (tbl_ele) {
        var staff_s_id = document.getElementsByName("staff_s_id");
        _tot_rec = document.getElementsByName("staff_s_id").length;
    }
    if (_sid.length > 0) {
        if ((parseInt(_sid.length) + parseInt(_tot_rec)) <= 20) {
            senddata_post("SalaryProcess", "T=getemployee_sal&Staffid=" + _sid + "&id=0&incwd=" + get_check_val("includeworkingdays") + '&incloan=' + get_check_val("includeloanamount") + '&month=' + document.getElementById("ddlMonth").value, "salgrid")
      } else {
      alert("You can add 20 employees at a time.And you already added 20 employees in the sheet.");
      }
  }
  else {
      alert("Please select employee first!");
  }
}
function Remove_Emp_from_List(staffid) {
    var ele = document.getElementById("emp_search_list");
    if (ele) {
        var eleTab = document.getElementsByName('chkEmployee');
        var div = ele.getElementsByTagName('div');
        if (eleTab) {
            for (var i = 0; i < eleTab.length; i++) {
                if (parseInt(eleTab[i].value) == parseInt(staffid)) {
                    var count = 0;
                    for (j = 0; j < div.length; j++) {
                        if (div[j].hasAttribute('class')) {
                            if (div[j].getAttribute("class").toString() == "emp_s_list") {
                                count = parseInt(count) + 1;
                            }
                        }
                        if ((parseInt(i) + 1) == parseInt(count)) {
                            try {
                                ele.removeChild(div[j]);
                            }
                            catch (e) {

                            }
                        }
                    }
                }
            }
        }
    }
}
function Setting() {
    $("#Setting").fadeIn("slow");
    $("#blank").fadeIn("slow");
}
function closeSetting() {
    $("#Setting").fadeOut("slow");
    $("#blank").fadeOut("slow");
}
//function check(eleId) {
//    var rVal='N'
//    if (document.getElementById(eleId)) {
//        if (document.getElementById(eleId).checked) {
//            rVal = 'Y';
//        }
//    }
//    return rVal;
//}

function clip() {

    // get the clipboard text
    var clipText = window.clipboardData.getData('Text');

    // split into rows

    clipRows = clipText.split(String.fromCharCode(13));

    // split rows into columns

    for (i = 0; i < clipRows.length; i++) {
        clipRows[i] = clipRows[i].split(String.fromCharCode(9));
    }


    // write out in a table

    newTable = document.createElement("table")
    newTable.border = 1;
    for (i = 0; i < clipRows.length - 1; i++) {

        newRow = newTable.insertRow();

        for (j = 0; j < clipRows[i].length; j++) {
            newCell = newRow.insertCell();
            if (clipRows[i][j].length == 0) {
                newCell.innerText = ' ';
            }
            else {
                newCell.innerText = clipRows[i][j];
            }
        }
    }
    alert(newTable);
 //   document.body.appendChild(newTable);
}
//document.onkeypress = function (e) {
//    var e = window.event || e
//    if (e.ctrlKey == true && (String.fromCharCode(e.charCode).toLowerCase() == 'v')) {
//        clip();
//    }
//}

function CreateExcel(sheetId, eleId) {
    senddata_post("SalaryProcess", "T=CreateExcel&Sheetid=" + sheetId, eleId);
}
function changestatus(sheetid, eleId, val,_operation) {
    call_password();
    var ele_pass = document.getElementById('password');
      if (ele_pass) {ele_pass.value = ""; }
    document.getElementById("e_id").value = eleId;
    document.getElementById("e_value").value = document.getElementById(eleId).innerHTML;
    document.getElementById("e_val").value = "&sh_id="+sheetid+"&sh_status="+val;
    O_refreshImage(eleId);
    if (_operation == "status") {
        document.getElementById('btnSubmit').setAttribute("onclick", "change_status();")
    }
    else {
        document.getElementById('btnSubmit').setAttribute("onclick", "check_Password_forEdit(" + sheetid + ", " + val + ",'" + _operation + "')")
    }
}
function closestatus() {
    close_Password();
    var eleid = document.getElementById("e_id").value;
    document.getElementById(eleid).innerHTML = document.getElementById("e_value").value;

}
function call_password() {
    $("#Password").fadeIn("fast");
    $("#blank").fadeIn("fast");
}
function close_Password() {
    $("#Password").fadeOut("fast");
    $("#blank").fadeOut("fast");
}
function change_status() {
    var ele_pass=document.getElementById('password');
    if (ele_pass) {
        if (ele_pass.value == "") {
            alert("Please enter password.");
            return;
        }
        senddata_post('SalaryProcess', 'T=ChangeStatus' + document.getElementById('e_val').value + "&pass=" + ele_pass.value + "&e_id=" + document.getElementById("e_id").value, document.getElementById("e_id").value);
        close_Password();
    }
}
function O_refreshImage(eleId) {
    if (document.getElementById(eleId)) {
        document.getElementById(eleId).innerHTML = "<div align='center'><img src='images/loading.gif' /></div>";
    }
}
function C_refreshImage(eleId) {
    if (document.getElementById(eleId)) {
        document.getElementById(eleId).innerHTML = "";
    }
}
function check_Password_forEdit(sh_id, r_index,_operation) {
    var ele_pass = document.getElementById('password');
    if (ele_pass) {
        if (ele_pass.value == "") {
            alert("Please enter password.");
            return;
        }
        senddata_post('SalaryProcess', 'T=checkpassword&sh_id=' + sh_id + "&pass=" + ele_pass.value + "&row_index=" + r_index, _operation);
        close_Password();
    }
}
function hide(addval,backval) {
    document.getElementById('btn_getEmployee').className = addval;
    document.getElementById('s_date').className = addval;
    document.getElementById('date').className = addval;
    document.getElementById('s_title').className = addval;
    document.getElementById('txtSheetTitle').className = addval;
    document.getElementById('save').className = addval;
    document.getElementById('new').className = addval;
    var _id = 0;
    _id = get_id();
    if (parseInt(_id) > 0) {
        document.getElementById('print').className = addval;
    }
    document.getElementById('ddlFY').className = backval;
    document.getElementById('back').className = backval;
    document.getElementById('copyfrom').className = addval;
    document.getElementById('formula_used').className = addval;
    document.getElementById('Search_function').className = backval;
}
function back_page() {
    hide("block", "none");
    Clear_all();
}
function print_sheet() {
    var ele = document.getElementById('SalarySheet');
    var content = "";
    var month = "";
    var date = "";
    var locationame="";
    if (document.getElementById('hdn_p_Title')) {
        sheet_title = document.getElementById('hdn_p_Title').value;
    }
    if (document.getElementById('hdn_p_Month')) {
        month = document.getElementById('hdn_p_Month').value;
    }
    if (document.getElementById('hdn_p_Date')) {
        date = document.getElementById('hdn_p_Date').value;
    }
    if (document.getElementById('hdn_p_content')) {
        content = document.getElementById('hdn_p_content').value;
    }
    if(document.getElementById('seslocname'))
    {
      locationame=document.getElementById('seslocname').value;
    }
    if (ele) {
        var str = "<style>@media print,screen{.page_break {page-break-after:always} body{text-transform:lowercase;text-transform:capitalize;} .cell{height:70px;width:90px;word-wrap:break-word; font-size:18pt;} .font{font-size:20pt;}}</style><div style='width:100%;height:40px;text-align:center' class='font'><b>" + locationame + "</b></div><div style='width:100%; text-align:center'><b>" + sheet_title + "<b><div style='float:left' class='font'>Month :" + month + " </div><div style='float:right' class='font'>Date :" + date + "</div></div><div>" + content + "</div>";
        var win = window.open("", "Salary Sheet", "width:40%");
        win.document.write(str);
        win.focus();
        win.print();  
    }
}
function get_Salary_sheet_value() {
    var str = "";
    var ele = document.getElementById("salList");
    if (ele) {
        for (var i = 1; i < ele.rows.length-1; i++) {
//            if (parseInt(i-1) == 0) {
//                str += "<table cellpadding='5' border='1' style='border-collapse:collapse' cellspacing='0' width='100%'>"
//                str += "<tr>";
//                for (var j = 1; j < ele.rows[0].cells.length; j++) {
//                    str += "<th>" + ele.rows[0].cells[j].innerHTML + "</th>";
//                }
//                str += "</tr>"
//            }
            if (parseInt(i) % 10 == 1) {
                var cls = "";
                if (parseInt((ele.rows.length) - parseInt(i)) > 12) {
                    var cls = "page_break";
                }
                str += "<table class='"+cls+"' cellpadding='5' border='1' style='border-collapse:collapse' cellspacing='0' width='100%'>"
                str += "<tr>";
                for (var j = 1; j < ele.rows[0].cells.length; j++) {
                    str += "<th><div  class='cell'>" + ele.rows[0].cells[j].innerHTML + "</div></th>";
                }
                str += "<th ><div class='cell'>Signature</div></th>"
                str+="</tr>"
            }
            str += "<tr>";
            for (var j = 1; j < ele.rows[i].cells.length; j++) {
                if (parseInt(i) == 0 || parseInt(i) == (parseInt(ele.rows.length)-1)) {
                    str += "<th>";
                }
                else {
                    str += "<td><div class='cell'>"
                }
                var ele_text = ele.rows[i].cells[j].getElementsByTagName('input')[0];
                if (ele_text) {
                    if (ele_text.type == "text") {
                        str += ele_text.value;
                    }
                    else {
                        str += ele.rows[i].cells[j].innerHTML;
                    }
                }
                else {
                    str += ele.rows[i].cells[j].innerHTML;
                }
                str += "</div></td>";
            }
            str+="<td>&nbsp;</td>"
            str += "</tr>";
            if (parseInt(i) % 10 == 0) {
                if (parseInt(i-1) != 0) {
                    str += "</table>";
                }
            }
        }
    }
        var ele_content = document.getElementById('hdn_p_content')
        var ele_title = document.getElementById('hdn_p_Title')
        var ele_month = document.getElementById('hdn_p_Month')
        var ele_date = document.getElementById('hdn_p_Date')
        var ele_p_title = document.getElementById('txtSheetTitle')
        var ele_p_month = document.getElementById('ddlMonth')
        var ele_p_date = document.getElementById('sDate')
        if (ele_content && ele_title && ele_month && ele_date && ele_p_date && ele_p_month && ele_p_title) {
            ele_content.value = str;
            ele_title.value = ele_p_title.value;
            ele_date.value = ele_p_date.value
            ele_month.value = ele_p_month.options[ele_p_month.selectedIndex].innerHTML;
        }
    return str;
}
function postingAccountLedger() {
    var arr_value = getSheetValue();
    if (arr_value.length > 0) {
        senddata_post('SalaryProcess', 'T=PostingInAccountledger&esid=' +arr_value,"PostingInAccountledger");
    } else {
        alert("Please select at least one sheet.");
    }
}
function deletePostedLedger() {
    var arr_value = getSheetValue();
    if (arr_value.length > 0) {
        senddata_post('SalaryProcess', 'T=Delete_PostingAccountledger&esid=' + arr_value, 'Delete_PostingAccountledger');
    } else {
        alert("Please select at least one sheet.");
    }
}
function sendEmail() {
    var arr_value = getSheetValue();
    if (arr_value.length > 0) {
        senddata_post('SalaryProcess', 'T=SendEmail&esid=' + arr_value, 'Delete_PostingAccountledger');
    } else {
        alert("Please select at least one sheet.");
    }
}
function sendSMS() {
    alert("Please purchase sms api for sending sms.");
}
function getSheetValue()
{
    var ele_table = document.getElementById('search_grid');
    var arr_value = [];
    if (ele_table) {
        var ele_input = ele_table.getElementsByTagName('input');
        for (var i = 0; i < ele_input.length; i++) {
            if (ele_input[i].type == "checkbox" && !(isNaN(ele_input[i].value)) && ele_input[i].checked) {
                arr_value.push(ele_input[i].value);
            }
        }
    }
    return arr_value;
}
//clearEmployee()
//function addClass(ele, class) {
//    if (document.getElementById(ele)) {
//        document.getElementById(ele).setAttribute("class",cl)
//    }
//}


